package org.PISS.utils;

import com.sun.faces.component.visit.FullVisitContext;

import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.component.visit.VisitCallback;
import javax.faces.component.visit.VisitContext;
import javax.faces.component.visit.VisitResult;
import javax.faces.context.FacesContext;

/**
 * Created with IntelliJ IDEA.
 * User: mafi0813
 * Date: 24.01.14
 * Time: 12:26
 * To change this template use File | Settings | File Templates.
 */
public class CommonUtil {
    /**
     *
     * @param shortID - the component id, like a id in XHTML document
     * @return full component id, like on client page for first component which has id = shortID
     */
    public static String getFullComponentID(final String shortID){
        FacesContext context = FacesContext.getCurrentInstance();
        UIViewRoot root = context.getViewRoot();
        final UIComponent[] found = new UIComponent[1];
        root.visitTree(new FullVisitContext(context), new VisitCallback() {
            @Override
            public VisitResult visit(VisitContext context, UIComponent component) {
                if(component.getId().equals(shortID)){
                    found[0] = component;
                    return VisitResult.COMPLETE;
                }
                return VisitResult.ACCEPT;
            }
        });
        return found[0].getClientId();
    }
}
